package com.pai4j.user.repository.dao;

import com.pai4j.user.repository.entity.GroupMemberEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface IGroupMemberDAO extends JpaSpecificationExecutor<GroupMemberEntity>, JpaRepository<GroupMemberEntity, Long> {

    /**
     * 根据群ID和用户账号查找群成员
     */
    GroupMemberEntity findByGroupPaiIdAndUserAccount(String groupPaiId, String userAccount);

    /**
     * 判断用户是否为群成员
     */
    boolean existsByGroupPaiIdAndUserAccount(String groupPaiId, String userAccount);

    /**
     * 根据群ID查找所有正常状态的成员
     */
    @Query("SELECT gm FROM GroupMemberEntity gm WHERE gm.groupPaiId = :groupPaiId AND gm.memberStatus IN (1, 2) ORDER BY gm.memberRole ASC, gm.joinTime ASC")
    List<GroupMemberEntity> findActiveMembers(@Param("groupPaiId") String groupPaiId);

    /**
     * 根据群ID和成员状态查找成员列表
     */
    List<GroupMemberEntity> findByGroupPaiIdAndMemberStatusIn(String groupPaiId, List<Integer> memberStatusList);

    /**
     * 根据群ID和成员角色查找成员列表
     */
    List<GroupMemberEntity> findByGroupPaiIdAndMemberRole(String groupPaiId, Integer memberRole);

    /**
     * 根据用户账号查找其加入的所有群聊
     */
    @Query("SELECT gm FROM GroupMemberEntity gm WHERE gm.userAccount = :userAccount AND gm.memberStatus IN (1, 2)")
    List<GroupMemberEntity> findUserGroups(@Param("userAccount") String userAccount);

    /**
     * 统计群成员数量（正常状态）
     */
    @Query("SELECT COUNT(gm) FROM GroupMemberEntity gm WHERE gm.groupPaiId = :groupPaiId AND gm.memberStatus IN (1, 2)")
    Long countActiveMembers(@Param("groupPaiId") String groupPaiId);

    /**
     * 统计群管理员数量
     */
    @Query("SELECT COUNT(gm) FROM GroupMemberEntity gm WHERE gm.groupPaiId = :groupPaiId AND gm.memberRole = 2 AND gm.memberStatus IN (1, 2)")
    Long countAdmins(@Param("groupPaiId") String groupPaiId);

    /**
     * 查找群主
     */
    @Query("SELECT gm FROM GroupMemberEntity gm WHERE gm.groupPaiId = :groupPaiId AND gm.memberRole = 1 AND gm.memberStatus IN (1, 2)")
    GroupMemberEntity findGroupOwner(@Param("groupPaiId") String groupPaiId);

    /**
     * 批量更新成员状态
     */
    @Modifying
    @Query("UPDATE GroupMemberEntity gm SET gm.memberStatus = :memberStatus, gm.updateDate = :updateDate WHERE gm.groupPaiId = :groupPaiId AND gm.userAccount IN :userAccounts")
    int batchUpdateMemberStatus(@Param("groupPaiId") String groupPaiId, 
                               @Param("userAccounts") List<String> userAccounts, 
                               @Param("memberStatus") Integer memberStatus, 
                               @Param("updateDate") Date updateDate);

    /**
     * 更新成员禁言状态
     */
    @Modifying
    @Query("UPDATE GroupMemberEntity gm SET gm.memberStatus = :memberStatus, gm.muteUntil = :muteUntil, gm.updateDate = :updateDate WHERE gm.groupPaiId = :groupPaiId AND gm.userAccount = :userAccount")
    int updateMemberMuteStatus(@Param("groupPaiId") String groupPaiId, 
                              @Param("userAccount") String userAccount, 
                              @Param("memberStatus") Integer memberStatus, 
                              @Param("muteUntil") Date muteUntil, 
                              @Param("updateDate") Date updateDate);

    /**
     * 更新成员角色
     */
    @Modifying
    @Query("UPDATE GroupMemberEntity gm SET gm.memberRole = :memberRole, gm.updateDate = :updateDate WHERE gm.groupPaiId = :groupPaiId AND gm.userAccount = :userAccount")
    int updateMemberRole(@Param("groupPaiId") String groupPaiId, 
                        @Param("userAccount") String userAccount, 
                        @Param("memberRole") Integer memberRole, 
                        @Param("updateDate") Date updateDate);

    /**
     * 删除群成员（物理删除，用于解散群聊）
     */
    void deleteByGroupPaiId(String groupPaiId);
    
    /**
     * 查询群主或管理员（用于获取审批人）
     * 按角色升序排列：群主(1)在前，管理员(2)在后
     */
    @Query("SELECT gm FROM GroupMemberEntity gm WHERE gm.groupPaiId = :groupPaiId AND gm.memberRole IN (1, 2) AND gm.memberStatus IN (1, 2) ORDER BY gm.memberRole ASC")
    List<GroupMemberEntity> findGroupAdminsAndOwner(@Param("groupPaiId") String groupPaiId);
}
